package org.example.jedis.set;

import org.example.jedis.JedisDemo;
import org.junit.Test;

/**
 * @author: tianhao.luo@hand-china.com 2021/5/25  15:22
 */
public class SetControlTest extends JedisDemo {

    /**
     * 命令:sadd key member1 member2 ...
     * 将一个或多个元素member添加到key对应的集合中，已存在的则被忽略。
     */
    @Test
    public void saddKeyMembers(){
        String key1 = "k1";
        String value1 = "Hello";
        String value2 = "World";
        String value3 = "Ok";
        delExistsKeyValue(key1);

        Long sadd = jedis.sadd(key1, value1, value2, value3);
        LOGGER.info("sadd的key为:{},执行的结果为:{}",key1,sadd);
    }

    /**
     * 命令:smembers key
     * 取出key对应集合的所有值
     */
    @Test
    public void smembersKey(){
        String key1 = "k1";
        String value1 = "Hello";
        String value2 = "World";
        String value3 = "Ok";
        delExistsKeyValue(key1);

        Long sadd = jedis.sadd(key1, value1, value2, value3);
        LOGGER.info("sadd的key为:{},执行的结果为:{}",key1,sadd);

        for (String smember : jedis.smembers(key1)) {
            LOGGER.info("set集合中元素为:{}",smember);
        }
    }

    /**
     * 命令:sismember key member
     * 判断key对应的集合中是否包含member元素，包含返回1，不包含返回0
     */
    @Test
    public void sismemberKeyMember(){
        String key1 = "k1";
        String value1 = "Hello";
        String value2 = "World";
        String value3 = "Ok";
        delExistsKeyValue(key1);

        Long sadd = jedis.sadd(key1, value1, value2, value3);
        LOGGER.info("sadd的key为:{},执行的结果为:{}",key1,sadd);

        Boolean sismember = jedis.sismember(key1, value2);
        LOGGER.info("{}对应的set集合中是否包含值:{},结果为:{}",key1,value2,sismember);
    }

    /**
     * 命令:scard key
     * 获取key对应集合的元素个数
     */
    @Test
    public void scardKey(){
        String key1 = "k1";
        String value1 = "Hello";
        String value2 = "World";
        String value3 = "Ok";
        delExistsKeyValue(key1);

        Long sadd = jedis.sadd(key1, value1, value2, value3);
        LOGGER.info("sadd的key为:{},执行的结果为:{}",key1,sadd);

        Long scard = jedis.scard(key1);
        LOGGER.info("{}对应的set集合中元素个数为:{}",key1,scard);
    }

    /**
     * 命令:srem key member1 member2 ...
     * 删除key对应集合中的member元素，删除成功时，返回已删除的元素个数；删除失败时返回0
     */
    @Test
    public void sremKeyMembers(){
        String key1 = "k1";
        String value1 = "Hello";
        String value2 = "World";
        String value3 = "Ok";
        delExistsKeyValue(key1);

        Long sadd = jedis.sadd(key1, value1, value2, value3);
        LOGGER.info("sadd的key为:{},执行的结果为:{}",key1,sadd);

        for (String smember : jedis.smembers(key1)) {
            LOGGER.info("set集合中元素为:{}",smember);
        }

        Long srem = jedis.srem(key1, value1, value2);
        LOGGER.info("srem的key为:{},执行的结果为:{}",key1,srem);
        for (String smember : jedis.smembers(key1)) {
            LOGGER.info("set集合中元素为:{}",smember);
        }
    }


    /**
     * 命令:spop key
     * 从key对应的集合中，随机吐出一个值
     */
    @Test
    public void spopKey(){
        String key1 = "k1";
        String value1 = "Hello";
        String value2 = "World";
        String value3 = "Ok";
        delExistsKeyValue(key1);

        Long sadd = jedis.sadd(key1, value1, value2, value3);
        LOGGER.info("sadd的key为:{},执行的结果为:{}",key1,sadd);

        for (String smember : jedis.smembers(key1)) {
            LOGGER.info("set集合中元素为:{}",smember);
        }

        String spop = jedis.spop(key1);
        LOGGER.info("spop的key为:{},执行的结果为:{}",key1,spop);

    }

    /**
     * 命令:srandmember key count
     * 从key对应的集合中，随机获取count个元素，但是不会从集合中移除他们
     */
    @Test
    public void srandmemberKeyCount(){
        String key1 = "k1";
        String value1 = "Hello";
        String value2 = "World";
        String value3 = "Ok";
        delExistsKeyValue(key1);

        Long sadd = jedis.sadd(key1, value1, value2, value3);
        LOGGER.info("sadd的key为:{},执行的结果为:{}",key1,sadd);

        for (String smember : jedis.smembers(key1)) {
            LOGGER.info("set集合中元素为:{}",smember);
        }

        int count = 2;
        for (String s : jedis.srandmember(key1, count)) {
            LOGGER.info("随机获取的元素为:{}",s);
        }

        for (String smember : jedis.smembers(key1)) {
            LOGGER.info("set集合中元素为:{}",smember);
        }
    }

    /**
     * 命令:smove source destination value
     * 从source对应的集合中移动value元素到destination对应的集合中
     */
    @Test
    public void smoveSourceDestinationValue(){
        String key1 = "k1";
        String value1 = "Hello";
        String value2 = "World";
        String value3 = "Ok";
        delExistsKeyValue(key1);

        Long sadd1 = jedis.sadd(key1, value1, value2, value3);
        LOGGER.info("sadd的key为:{},执行的结果为:{}",key1,sadd1);

        for (String smember : jedis.smembers(key1)) {
            LOGGER.info("key:{}对应的set集合中元素为:{}",key1,smember);
        }

        String key2 = "k2";
        String value4 = "Java";
        String value5 = "Mysql";
        String value6 = "C";
        delExistsKeyValue(key2);

        Long sadd2 = jedis.sadd(key2, value4, value5, value6);
        LOGGER.info("sadd的key为:{},执行的结果为:{}",key2,sadd2);

        for (String smember : jedis.smembers(key2)) {
            LOGGER.info("key:{}对应的set集合中元素为:{}",key2,smember);
        }

        Long smove = jedis.smove(key1, key2, value2);
        LOGGER.info("从原key:{}到目标key:{},移动元素:{},执行结果为:{}",key1,key2,value2,smove);

        for (String smember : jedis.smembers(key1)) {
            LOGGER.info("key:{}对应的set集合中元素为:{}",key1,smember);
        }

        for (String smember : jedis.smembers(key2)) {
            LOGGER.info("key:{}对应的set集合中元素为:{}",key2,smember);
        }
    }


    /**
     * 命令:sinter key1 key2
     * 取key1和key2对应集合的交集
     */
    @Test
    public void sinterKeys(){
        String key1 = "k1";
        String value1 = "Hello";
        String value2 = "World";
        String value3 = "Ok";
        delExistsKeyValue(key1);

        Long sadd1 = jedis.sadd(key1, value1, value2, value3);
        LOGGER.info("sadd的key为:{},执行的结果为:{}",key1,sadd1);

        for (String smember : jedis.smembers(key1)) {
            LOGGER.info("key:{}对应的set集合中元素为:{}",key1,smember);
        }

        String key2 = "k2";
        String value4 = "Hello";
        String value5 = "Mysql";
        String value6 = "C";
        delExistsKeyValue(key2);

        Long sadd2 = jedis.sadd(key2, value4, value5, value6);
        LOGGER.info("sadd的key为:{},执行的结果为:{}",key2,sadd2);

        for (String smember : jedis.smembers(key2)) {
            LOGGER.info("key:{}对应的set集合中元素为:{}",key2,smember);
        }

        for (String s : jedis.sinter(key1, key2)) {
            LOGGER.info("key取交集的内容为:{}",s);
        }
    }


    @Test
    public void sunionKeys(){
        String key1 = "k1";
        String value1 = "Hello";
        String value2 = "World";
        String value3 = "Ok";
        delExistsKeyValue(key1);

        Long sadd1 = jedis.sadd(key1, value1, value2, value3);
        LOGGER.info("sadd的key为:{},执行的结果为:{}",key1,sadd1);

        for (String smember : jedis.smembers(key1)) {
            LOGGER.info("key:{}对应的set集合中元素为:{}",key1,smember);
        }

        String key2 = "k2";
        String value4 = "Hello";
        String value5 = "Mysql";
        String value6 = "C";
        delExistsKeyValue(key2);

        Long sadd2 = jedis.sadd(key2, value4, value5, value6);
        LOGGER.info("sadd的key为:{},执行的结果为:{}",key2,sadd2);

        for (String smember : jedis.smembers(key2)) {
            LOGGER.info("key:{}对应的set集合中元素为:{}",key2,smember);
        }

        for (String s : jedis.sunion(key1, key2)) {
            LOGGER.info("key取并集的内容为:{}",s);
        }

    }


    /**
     * 命令:sdiff key1 key2
     * 返回两个集合的差集元素（key1对应集合中不包含key2对应集合的）
     */
    @Test
    public void sdiffKeys(){
        String key1 = "k1";
        String value1 = "Hello";
        String value2 = "World";
        String value3 = "Ok";
        delExistsKeyValue(key1);

        Long sadd1 = jedis.sadd(key1, value1, value2, value3);
        LOGGER.info("sadd的key为:{},执行的结果为:{}",key1,sadd1);

        for (String smember : jedis.smembers(key1)) {
            LOGGER.info("key:{}对应的set集合中元素为:{}",key1,smember);
        }

        String key2 = "k2";
        String value4 = "Hello";
        String value5 = "Mysql";
        String value6 = "C";
        delExistsKeyValue(key2);

        Long sadd2 = jedis.sadd(key2, value4, value5, value6);
        LOGGER.info("sadd的key为:{},执行的结果为:{}",key2,sadd2);

        for (String smember : jedis.smembers(key2)) {
            LOGGER.info("key:{}对应的set集合中元素为:{}",key2,smember);
        }

        for (String s : jedis.sdiff(key1, key2)) {
            LOGGER.info("key取差集的内容为:{}",s);
        }
    }
}
